package ee.nlp.evn.entities;

import java.util.ArrayList;
import java.util.List;

public class Sentence {
	private String source;
	private List<Word> words;
	
	public Sentence() {
		source = "";
		words = new ArrayList<Word>();
	}

	public String getSource() {
		return source;
	}

	public void setSource(String source) {
		this.source = source;
	}

	/**
	 * @return the words
	 */
	public List<Word> getWords() {
		return words;
	}

	public void setWords(ArrayList<Word> words) {
		this.words = words;
	}
	
	/**
	 * @param words the words to set
	 */
	public void addWord(Word word) {
		this.words.add(word);
	}

	public void addOLP(String type) {
		Word word = new Word("");
		word.addTag("OLP", type);
		this.words.add(word);
	}

	public String getOutputForm() {
		String result = "";
		for(Word w : words) {
			result += (result.isEmpty()? "" : " ") + w.getUsedForm();
		}
		return result;
	}

	public String getTagForm() {
		String result = "";
		for(Word w : words) {
			result += w.getTagForm() + "\n";
		}
		return result;
	}

	public boolean includesTag(String tag, String value) {
		boolean result = false;
		for(Word w : words) {
			if (w.includeTag(tag, value)) {
				result = true;
				break;
			}
		}
		return result;
	}

	public Word after(Word w) {
		int i = words.indexOf(w);
		if (i >= 0 && words.size() >= i + 2) return words.get(i+1);
		return new Word("");
	}

	public Word before(Word w) {
		int i = words.indexOf(w);
		if (i > 0 ) return words.get(i-1);
		return new Word("");
	}
	
	/*
	 * Fn tagastab osalausete loetelu.
	 */
	public ArrayList<Sentence> getPartials() {
		ArrayList<Sentence> result = new ArrayList<Sentence>();
		Sentence curr = new Sentence();
		Sentence kiil = new Sentence();
		boolean inKiil = false;
		for (Word w : words) {
			if (w.isOLP("P")) {
				result.add(curr);
				curr = new Sentence();
			} else if (w.isOLP("KA")) {
				inKiil = true;
			} else if (w.isOLP("KL")) {
				inKiil = false;
				result.add(kiil);
				kiil = new Sentence();
			} else if (inKiil) {
				kiil.addWord(w);
			} else {
				curr.addWord(w);
			}
		}
		if (!curr.words.isEmpty()) {
			result.add(curr);
		}
		if (!kiil.words.isEmpty()) {
			result.add(kiil);
		}
		return result;
	}
}
